package com.lzm.ydpt.module.order.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.byt.framlib.basemvp.basebean.ListPageBean;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonwidget.LoadingTip;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.refresh.RefreshHeaderView;
import com.byt.framlib.commonwidget.selectordialog.dialog.DialogOnClickListener;
import com.byt.framlib.commonwidget.selectordialog.dialog.NormalAlertDialog;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.Dapplication;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.AddressBook;
import com.lzm.ydpt.module.order.activity.AddAddressBookActivity;
import com.lzm.ydpt.mvp.contract.AddressBookListContract;
import com.lzm.ydpt.mvp.presenterimpl.AddressBookListPresenterImpl;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 地址簿
 */
public class AddressBookFragment extends BaseFragment<AddressBookListPresenterImpl> implements AddressBookListContract.View {

    @BindView(R.id.ltp_address_book_list)
    LoadingTip ltp_address_book_list;
    @BindView(R.id.srf_address_book_list)
    RefreshLayout srf_address_book_list;
    @BindView(R.id.rv_address_book_list)
    RecyclerView rv_address_book_list;

    private static AddressBookFragment sFragment = null;
    private RvCommonAdapter<AddressBook> adapter = null;
    private List<AddressBook> mList = new ArrayList<>();
    private int pageNum = 1;

    /**
     * 0 == 直接进来
     * 1 == 选择地址进来
     */
    private int jumpType = 0;

    /**
     * 1 收货地址
     * 2 发货地址
     */
    private int type = 1;

    /**
     * 发货地址
     * 送货地址
     *
     * @param type
     * @return
     */
    public static AddressBookFragment newsInstance(int type, int jumpType) {
        sFragment = new AddressBookFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.INP_APPEND_ADDRESS_TYPE, type);
        bundle.putInt(Constants.INP_ADDRESS_MANAGER_TYPE, jumpType);
        sFragment.setArguments(bundle);
        return sFragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_address_book_list;
    }

    @Override
    public AddressBookListPresenterImpl initPresenter() {
        return new AddressBookListPresenterImpl(this);
    }

    @Override
    protected void initView() {
        type = getArguments().getInt(Constants.INP_APPEND_ADDRESS_TYPE, 1);
        jumpType = getArguments().getInt(Constants.INP_ADDRESS_MANAGER_TYPE, 0);
        initRv();
        initAdapter();
        ltp_address_book_list.setLoadingTip(LoadingTip.LoadStatus.loading);
        fetchData();
    }

    private void initRv() {
        setRefreshLayout(srf_address_book_list);
        srf_address_book_list.setRefreshHeader(new RefreshHeaderView(context).getHeaderStyleStaffF4());
        srf_address_book_list.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                fetchData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                fetchData();
            }
        });
    }

    private void initAdapter() {
        rv_address_book_list.setLayoutManager(new LinearLayoutManager(context));
        adapter = new RvCommonAdapter<AddressBook>(context, mList, R.layout.item_address_book_rv) {
            @Override
            protected void convert(RvViewHolder holder, AddressBook addressBook, int position) {
                holder.setText(R.id.tv_user_mail_info, addressBook.getContacts() + "  " + addressBook.getPhone());
                holder.setText(R.id.tv_user_mail_address, addressBook.getAddress());
                holder.setOnClickListener(R.id.ll_edit_address_book, new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        //编辑
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(Constants.INP_APPEND_ADDRESS_BEAN, addressBook);
                        bundle.putInt(Constants.INP_APPEND_ADDRESS_TYPE, 2);
                        openActivity(AddAddressBookActivity.class, bundle);
                    }
                });
                holder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        if (jumpType == 1) {
                            Bundle bundle = new Bundle();
                            bundle.putParcelable(Constants.INP_APPEND_ADDRESS_BEAN, addressBook);
                            setResultOk(bundle);
                        }
                    }
                });
                holder.setOnClickListener(R.id.ll_del_address_book, new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        NormalAlertDialog normalAlertDialog = new NormalAlertDialog.Builder(context)
                                .setButtonTextSize(14)
                                .setTitleVisible(false)
                                .setTitleTextSize(16)
                                .setContentText("确定要删除该地址吗？")
                                .setContentTextSize(14)
                                .setContentTextColor(R.color.color_333333)
                                .setOnclickListener(new DialogOnClickListener() {

                                    @Override
                                    public void clickLeftButton(View view) {

                                    }

                                    @Override
                                    public void clickRightButton(View view) {
                                        deleteData(addressBook.getId(), position);
                                    }
                                }).build();
                        normalAlertDialog.show();
                    }
                });
            }
        };
        rv_address_book_list.setAdapter(adapter);
    }

    private void fetchData() {
        FormBodys.Builder formBodys = new FormBodys.Builder();
        formBodys.add("memberId", Dapplication.getUserInfo().getMemberId());
        formBodys.add("type", type);
        formBodys.add("pageSize", 10);
        formBodys.add("pageNum", pageNum);
        mPresenter.onAddressBookList(formBodys.build());
    }

    private void deleteData(long address_id, int position) {
        startProgressDialog();
        FormBodys.Builder formBodys = new FormBodys.Builder();
        formBodys.add("id", address_id);
        mPresenter.onDeleteAddress(formBodys.build(), position);
    }

    @Override
    public void onDestroyView() {
        sFragment = null;
        super.onDestroyView();
    }

    @Override
    public void onAddressBookList(ListPageBean<AddressBook> mAddressBookList) {
        if (pageNum == 1) {
            mList.clear();
            srf_address_book_list.finishRefresh();
        } else {
            srf_address_book_list.finishLoadMore();
        }
        mList.addAll(mAddressBookList.getList());
        adapter.notifyDataSetChanged();
        srf_address_book_list.setEnableLoadMore(mAddressBookList.getList().size() >= 10);
        ltp_address_book_list.setLoadingTip(mList.size() > 0 ? LoadingTip.LoadStatus.finish : LoadingTip.LoadStatus.empty);
    }

    @Override
    public void onDeleteAddress(String msg, int position) {
        stopProgressDialog();
        showShortToast(msg);
        mList.remove(position);
        adapter.notifyDataSetChanged();
        ltp_address_book_list.setLoadingTip(mList.size() > 0 ? LoadingTip.LoadStatus.finish : LoadingTip.LoadStatus.empty);
    }

    @Override
    public void showMessage(String message, String method) {
        stopProgressDialog();
        showShortToast(message);
    }

    @Override
    public void showErrorView(String msg) {
        showErrorView(msg, ltp_address_book_list);
        ltp_address_book_list.setOnReloadListener(new LoadingTip.onReloadListener() {
            @Override
            public void reload(View view) {
                ltp_address_book_list.setLoadingTip(LoadingTip.LoadStatus.loading);
                fetchData();
            }
        });
    }
}